import logging
import pyvisa

# GPIB 控制 2303 电源的配置
class PowerSupplyController:
    def __init__(self, gpib_address):
        """
        初始化 PowerSupplyController 类,用于控制 GPIB 连接的电源。

        :param gpib_address: 电源的 GPIB 地址,格式为 'GPIB0::[具体地址]::INSTR'
        """
        self.gpib_address = gpib_address
        self.rm = pyvisa.ResourceManager()
        self.power_supply = self.rm.open_resource(self.gpib_address)

    def turn_on_power(self):
        """
        打开电源
        """
        self.power_supply.write('OUTP ON')

    def turn_off_power(self):
        """
        关闭电源
        """
        self.power_supply.write('OUTP OFF')

    def set_voltage(self, voltage):
        """
        设置电源输出电压

        :param voltage: 要设置的电压值
        """
        self.power_supply.write(f'VOLT {voltage}')
        logging.info(f"Set voltage to {voltage}")

    def set_current(self, current):
        """
        设置电源输出电流

        :param current: 要设置的电流值
        """
        self.power_supply.write(f'CURR {current}')
    

# 示例：创建 PowerSupplyController 实例并使用
if __name__ == "__main__":
    gpib_address = 'GPIB0::16::INSTR'  # 请替换为实际的 GPIB 地址
    controller = PowerSupplyController(gpib_address)
    controller.turn_on_power()
    controller.set_voltage(5)
    controller.set_current(1)